import pandas as pd
import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif'] = 'SimHei'
plt.rcParams['axes.unicode_minus'] = False
plt.figure(figsize=(16, 10))


def floorView(df):
	counts = df.groupby(by="楼层").count()
	plt.title("各个楼层的房源数量分析")
	plt.bar(counts.index, counts["标题"], label="数量")
	plt.xticks(rotation=45)
	plt.ylabel("数量")
	plt.legend()
	plt.show()


def houseBarView(df):
	counts = df["户型"].value_counts().sort_index()
	plt.bar(counts.index, counts.values)
	plt.ylabel("数量")
	plt.title("户型统计")
	plt.xticks(rotation=20)
	plt.grid(axis="y")
	plt.show()


def directionView(df):
	counts = df.groupby(by="朝向").count()  # 统计
	plt.title("朝向统计")
	plt.bar(counts.index, counts["标题"], label="数量")
	plt.xticks(rotation=90)
	plt.ylabel("数量")
	plt.legend()
	plt.show()


def doubleView(df):
	df["单价"] = pd.to_numeric(df["单价"], errors='coerce')
	df["总价"] = pd.to_numeric(df["总价"], errors='coerce')
	counts = df["单价"].groupby(df["时间"]).median()  # 统计
	fig = plt.figure()
	ax1 = fig.add_subplot(111)
	ax1.set_ylabel("元/平米")
	ax1.bar(counts.index, counts.values, label="平均单价（元/平米）")
	ax2 = ax1.twinx()
	counts = df["总价"].groupby(df["时间"]).median()  # 统计
	ax2.plot(counts.index, counts.values, label="总价（万）", color="red")
	ax2.set_ylabel("万(元)")
	fig.legend(loc=1, bbox_to_anchor=(1, 1), bbox_transform=ax1.transAxes)  # 防止图例变形
	plt.title("不同年代的房屋单价总价分析")
	plt.show()


def areaPieView(df):
	# 分割面积数据为不同的区间
	divide = [0, 90, 100, 110, 120, 140, 160, 1200]
	# 获取面积列数据
	cut = pd.cut(list(df["面积"]), divide)
	data = cut.describe()
	# 绘制扇形图
	plt.pie(x=data.freqs, labels=data.index, autopct="%1.2f%%", explode=[0, 0, 0, 0, 0, 0, 0.2])
	plt.legend(loc='upper right')
	plt.title("面积占比分析")
	plt.show()
